﻿
using ML.Entity;
using ML.Utils;
using System;
using System.Web.Mvc;

namespace ML.Web.Controllers
{
    /// <summary>
    /// 拦截器，后端判断用户登录信息(如果用户没有登录或者没有权限访问某些菜单，则提示错误)
    /// </summary>
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
    public class LoginAttribute : ActionFilterAttribute
    {
        /// <summary>
        /// 验证登录等信息
        /// </summary>
        /// <param name="context"></param>
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            if (context.HttpContext == null || context.HttpContext.Session == null)
            {
                throw new Exception("此特性只适合于Web应用程序使用或者您的服务器Session不可用！");
            }

            //首先判断用户是否登陆读取到Session，如果没有读取则直接返回登陆页面
            UserInfoSession userInfoSession = UserHelper.GetCurrentUser();
            if (userInfoSession == null)
            {
                context.Result = new RedirectResult("/Login/Index");
            }
            else
            {
                base.OnActionExecuting(context);
            }
        }
    }
}